﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;

namespace TSharp.Core.Asm
{
    /// <summary>
    /// 类类型初始化扩展点
    /// <para>2010/12/24</para>
    /// 	<para>THINKPADT61</para>
    /// 	<para>tangjingbo</para>
    /// </summary>
    public class TypeInitExtensionPoint : ExtensionPoint<TypeInitAttribute>
    {
        private readonly List<Type> _types = new List<Type>();

        /// <summary>
        /// Registers the specified assembly.
        /// </summary>
        /// <param name="assembly">The assembly.</param>
        /// <param name="attribute">The attribute.</param>
        public override void Register(Assembly assembly, TypeInitAttribute attribute)
        {
            _types.Add(attribute.InitType);
        }

        /// <summary>
        /// Uns the register.
        /// </summary>
        /// <param name="assembly">The assembly.</param>
        /// <param name="attribute">The attribute.</param>
        public override void UnRegister(Assembly assembly, TypeInitAttribute attribute)
        {
        }

        /// <summary>
        /// 加载时执行
        /// </summary>
        public override void OnLoad()
        {
            foreach (Type initType in _types)
            {
                try
                {
                    //这样调度静态构造是不会被触发2次的。
                    RuntimeHelpers.RunClassConstructor(initType.TypeHandle);
                    /*
                     * 如果initType是抽象的，cctor将不会被执行
                     * 如果initType是继承自某类，如果没有自己的cctor或者静态成员访问父类，则父类的cctor不会执行                    * 
                     */
                }
                catch (TypeInitializationException ex)
                {
                    Debug.WriteLine(ex.ToString());
                }
            }
            _types.Clear();
            base.OnLoad();
        }
    }
}